package com.powernode.springmvc.controller;

import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.file.Files;
import java.util.UUID;

/**
 * @Author: 曾铖坚
 * @Date: 2024/10/8 17:55
 * @Version: 1.0.0
 */
@Controller
public class FileController {
    @GetMapping("/download")
    public ResponseEntity<byte[]> downloadFile(HttpServletResponse response, HttpServletRequest request) throws IOException {
        File file = new File(request.getServletContext().getRealPath("/upload") + "/依赖倒置原则.png");
        // 创建响应头对象
        HttpHeaders headers = new HttpHeaders();
        // 设置响应内容类型
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        // 设置下载文件的名称
        headers.setContentDispositionFormData("attachment", file.getName());

        // 下载文件
        return new ResponseEntity<byte[]>(Files.readAllBytes(file.toPath()), headers, HttpStatus.OK);
    }

    @RequestMapping(value = "/fileup", method = RequestMethod.POST)
    public String fileUp(@RequestParam("fileName") MultipartFile multipartFile, HttpServletRequest request) throws IOException {
        // 获取请求参数的name
        String name = multipartFile.getName();
        System.out.println(name);// fileName
        // 获取文件的真实名字
        String originalFilename = multipartFile.getOriginalFilename();
        System.out.println(originalFilename);// 依赖倒置原则.png

        // 输入流，负责读客户端的文件
        InputStream is = multipartFile.getInputStream();
        // 封装成缓冲流
        BufferedInputStream bis = new BufferedInputStream(is);

        ServletContext application = request.getServletContext();
        String realPath = application.getRealPath("/upload");
        File file = new File(realPath);
        if (!file.exists()) {
            file.mkdirs();
        }
        //File desFile = new File(file.getAbsolutePath() + "/" + originalFilename);
        File desFile = new File(file.getAbsolutePath() + "/" + UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf(".")));

        // 输出流
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desFile));

        // 一边读，一边写
        byte[] bytes = new byte[1024 * 10000];
        int count = 0;
        while((count = bis.read(bytes)) != -1){
            bos.write(bytes,0,count);
        }

        bos.flush();
        bos.close();
        bis.close();

        return "ok";
    }
}
